package com.testdocs.client.project.context;

import com.google.gwt.event.shared.EventBus;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.project.event.ProjectDeselectedEvent;
import com.testdocs.client.project.event.ProjectDeselectedEventHandler;
import com.testdocs.client.project.event.ProjectRemovedEvent;
import com.testdocs.client.project.event.ProjectRemovedEventHandler;
import com.testdocs.client.project.event.ProjectSelectedEvent;
import com.testdocs.client.project.event.ProjectSelectedEventHandler;

public class ProjectContextHolder {
	private ProjectInfo project;
	
	public ProjectContextHolder(EventBus eventBus) {
		eventBus.addHandler(ProjectSelectedEvent.TYPE, new ProjectSelectedHandler());
		eventBus.addHandler(ProjectDeselectedEvent.TYPE, new ProjectDeselectedHandler());
		eventBus.addHandler(ProjectRemovedEvent.TYPE, new ProjectRemovedHandler());
	}

	public ProjectInfo getProject() {
		return project;
	}

	private void setProject(ProjectInfo project) {
		this.project = project;
	}
	
	public class ProjectSelectedHandler implements ProjectSelectedEventHandler {
		@Override
		public void projectSelected(ProjectSelectedEvent event) {
			setProject(event.getProjectInfo());
		}
	}
	
	public class ProjectDeselectedHandler implements ProjectDeselectedEventHandler {
		@Override
		public void projectDeselected(ProjectDeselectedEvent event) {
			setProject(null);
		}
	}

	public class ProjectRemovedHandler implements ProjectRemovedEventHandler {
		@Override
		public void projectRemoved(ProjectRemovedEvent event) {
			setProject(null);
		}
	}
}
